package com.bckj.freshnow.payment.pay;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@RequiredArgsConstructor
public class PaymentServiceManager {

    private final PaymentServiceFactory paymentServiceFactory;
    private final Map<String, PaymentService> paymentServiceMap = new ConcurrentHashMap<>();

    public PaymentService getPaymentService(String appId, String paymentMethod) {
        return paymentServiceMap.computeIfAbsent(buildKey(appId, paymentMethod), key1 -> paymentServiceFactory.getPaymentService(appId, paymentMethod));
    }

    private String buildKey(String appId, String paymentMethod) {
        return appId + ":" + paymentMethod;
    }

}
